package com.jh.paymentchannel.business.impl;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;

import com.jh.paymentchannel.pojo.RealNameAuth;
import com.jh.paymentchannel.pojo.RealNameAuthRoute;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jh.paymentchannel.business.RealnameAuthBusiness;
import com.jh.paymentchannel.repository.HLRegisterRepository;
import com.jh.paymentchannel.repository.JFApiRepository;
import com.jh.paymentchannel.repository.JFCoinRepository;
import com.jh.paymentchannel.repository.JFRepository;
import com.jh.paymentchannel.repository.MiMaiRegisterRepository;
import com.jh.paymentchannel.repository.RealnameAuthRepository;
import com.jh.paymentchannel.repository.RealnameAuthRouteRepository;
import com.jh.paymentchannel.repository.SDJBindCardRepository;
import com.jh.paymentchannel.repository.SDJRepository;
import com.jh.paymentchannel.util.PaymentChannelConstants;

@Service
public class RealnameAuthBusinessImpl implements RealnameAuthBusiness{


	@Autowired
	private RealnameAuthRepository realNameAuthrepository;

	@Autowired
	private RealnameAuthRouteRepository realNameAuthRouterepository;

	@Autowired
	private HLRegisterRepository hLRegisterRepository;

	@Autowired
	private JFCoinRepository jFCoinRepository;

	@Autowired
	private JFRepository jFRepository;

	@Autowired
	private JFApiRepository jFApiRepository;

	@Autowired
	private MiMaiRegisterRepository miMaiRegisterRepository;

	@Autowired
	private SDJBindCardRepository sDJBindCardRepository;

	@Autowired
	private SDJRepository sDJRepository;

	@Autowired
	private EntityManager em;

	private static final Logger LOG = LoggerFactory.getLogger(RealnameAuthBusinessImpl.class);

	@Override
	public List<RealNameAuth> findAllRealnames(Pageable pageable) {
		Page<RealNameAuth> page =  realNameAuthrepository.findAll(pageable);
		return page.getContent();
	}

	/**验证身份证号与姓名是否匹配*/
	@Transactional
	@Override
	public RealNameAuth realNameAuth(String realname, String idcard, long userid,String jhKey) {

		RealNameAuthRoute realNameAuthRoute = realNameAuthRouterepository.getCurActiveRealNameAuthChannel();

		String realnameChannel = realNameAuthRoute.getCurChannel();
		String result = "";
		if(realnameChannel.equalsIgnoreCase(PaymentChannelConstants.REALNAME_CHANNEL_1)){
			LOG.info("=====请求聚合实名认证接口");
			result = JuHeAPIRealnameAuthService.realNameAuth(idcard, realname,jhKey);
		}

		/**如果userid已经存在那么直接更新*/
		List<RealNameAuth>  realnameList = realNameAuthrepository.findRealNameByUserId(userid);

		RealNameAuth realNameAuth = null;

		if(realnameList.size()>0){
			realNameAuth=realnameList.get(0);
		}else{
			realNameAuth= null;
		}

		if(realNameAuth != null ){
			realNameAuth.setRealname(realname);
			realNameAuth.setIdcard(idcard);
			realNameAuth.setAuthTime(new Date());

		}else{

			realNameAuth = new RealNameAuth();
			realNameAuth.setAuthTime(new Date());
			realNameAuth.setIdcard(idcard);
			realNameAuth.setRealname(realname);
			realNameAuth.setUserId(userid);

		}

		JSONObject jsonObject =  JSONObject.fromObject(result);
		JSONObject resObject  =  jsonObject.getJSONObject("result");
		if(resObject == null || resObject.isNullObject()){
			resObject = new JSONObject();
		}
		if(resObject.has("realname")){
			realNameAuth.setResult(resObject.getString("res"));
			if(resObject.getString("res").equalsIgnoreCase("1")){
				realNameAuth.setMessage("匹配");
			}else{
				realNameAuth.setMessage("不匹配");
			}

		}else{
			realNameAuth.setResult("2");
			realNameAuth.setMessage("不匹配");
		}

		RealNameAuth tempResult =  realNameAuthrepository.save(realNameAuth);

		em.flush();
		return tempResult;
	}


	@Override
	public RealNameAuth findRealNamesByIdcard(String idcard) {
		return realNameAuthrepository.findRealNameByIdcard(idcard);
	}

	@Override
	public RealNameAuth findRealNamesByall(long userid,String idcard,String realname) {
		return realNameAuthrepository.findRealNamesByall(userid,idcard,realname);
	}

	@Override
	public List<RealNameAuth> findRealNamesAuthByName(String realname) {
		return realNameAuthrepository.findRealNameByName(realname);
	}

	@Transactional
	@Override
	public RealNameAuth findRealNamesAuthByUsserId(long userid){
		List<RealNameAuth> realnameList=realNameAuthrepository.findRealNameByUserId(userid);
		if(realnameList.size()>0){
			return realnameList.get(0);
		}
		return new RealNameAuth();
	}

	//注销用户记录
	@Transactional
	@Override
	public void delRealnameByUserid(long userid) {
		realNameAuthrepository.delRealnameByUserid(userid);
		hLRegisterRepository.delHLRegisterByUserid(userid+"");
		jFCoinRepository.delJFCoinRegisterByUserid(userid);
		jFRepository.delJFRegisterByUserid(userid);
		jFApiRepository.delJFApiRegisterByUserid(userid);
		miMaiRegisterRepository.delMiMaiRegisterByUserid(userid);
		sDJBindCardRepository.delSDJBindCardByUserid(userid+"");
		sDJRepository.delSDJRegisterByUserid(userid);
	}

	@Override
	public List<RealNameAuth> findRealNamesAuthByUserIds(long[] userIds) {
		return realNameAuthrepository.findRealNamesAuthByUserIds(userIds);
	}

	@Override
	public RealNameAuth findRealNamesAuthByUserId(long userId) {
		return realNameAuthrepository.findByUserId(userId);
	}

	@Override
	public void addRealnameAuth(RealNameAuth realnameAuth) {
		realNameAuthrepository.save(realnameAuth);
		em.clear();

	}

	@Transactional
	@Override
	public void updateRealnameMsg(long userId, String result) {
		realNameAuthrepository.updateResultById(userId, result);
	}

	@Override
	public RealNameAuth findRealnameAuthById(long userId) {
		RealNameAuth realnameAuth = realNameAuthrepository.findRealnameAuthById(userId);
		return realnameAuth;
	}

}
